package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import com.ruicar.afs.cloud.afscase.channel.entity.ChannelWitnessInfo;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelWitnessInfoService;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Ice
 * @date 2020/8/25 13:49
 */
@AllArgsConstructor
@Slf4j
@Component
public class WitnessProcessor implements AfsMqBizProcessor<ChannelWitnessInfo> {
    private final ChannelWitnessInfoService channelWitnessInfoService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(ChannelWitnessInfo entity) throws Exception {
        ChannelWitnessInfo c = channelWitnessInfoService.getById(entity.getId());
        if (c == null) {
            channelWitnessInfoService.save(entity);
            if(!channelWitnessInfoService.save(entity)){
                throw new AfsBaseException("案件添加见证人失败！");
            }
        } else {
            channelWitnessInfoService.updateById(entity);
            if(!channelWitnessInfoService.updateById(entity)){
                throw new AfsBaseException("案件修改见证人失败！");
            }
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_CHANNEL_CHANNEL_CASE_CTM_SAVE_WITNESS;
    }
}
